package com.yy.ssjx.controller;

import cn.hutool.core.lang.UUID;
import com.yy.ssjx.domain.ShardingXaTest1;
import com.yy.ssjx.domain.ShardingXaTest2;
import com.yy.ssjx.mapper.ShardingXaTest1Mapper;
import com.yy.ssjx.mapper.ShardingXaTest2Mapper;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 描述: sharding-jdbc-xa分布式事务对象
 *
 * @author: yanglin
 * @Date: 2021-11-08-14:10
 * @Version: 1.0
 */
@Slf4j
@GlobalTransactional(rollbackFor = Exception.class)
@RestController
@RequestMapping("/api/tx")
public class ShardingSeataTestController {

    @Autowired
    private ShardingXaTest1Mapper xaTest1Mapper;

    @Autowired
    private ShardingXaTest2Mapper xaTest2Mapper;

    @Transactional
    @GetMapping("/saveSeata")
    public String insertSeataTest(){
        // d_sharding_xa_test1表在(ds0)的lth库
        ShardingXaTest1 xaTest1 = new ShardingXaTest1();
        xaTest1.setXaName(UUID.randomUUID().toString()+"-base");
        xaTest1.setXaRemark(UUID.randomUUID().toString()+"-basemark");
        xaTest1Mapper.insert(xaTest1);
        // d_sharding_xa_test2表在(ds1)的mumu库
        ShardingXaTest2 xaTest2 = new ShardingXaTest2();
        xaTest2.setXaName(UUID.randomUUID().toString()+"-base");
        xaTest2.setXaRemark(UUID.randomUUID().toString()+"-basemark");
        xaTest2Mapper.insert(xaTest2);
        return "success";
    }
}
